// pages/classic/classic.js
import {ClassicModel} from '../../models/classic.js'
import { LikeModel } from '../../models/like.js'
let classicModel = new ClassicModel()
let likeModel = new LikeModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    classicData: null,
    latest: true,
    first: false,
    likeCount:0,
    likeStatus:false
  },
  
  onLoad: function(options) {
    classicModel.getLatest((data) => {
      // setData数据更新
      // this._getLikeStatus(res.id,res.type)
      this.setData({
        classicData: data,
        likeCount: data.fav_nums,
        likeStatus: data.like_status
      })
      // setdata里面的数据已经添加至data里面
    })
  },
  onLike: function (event) {
    // console.log(this.data.classicData.id)
    const behavior = event.detail.behavior
    likeModel.like(behavior, this.data.classicData.id, this.data.classicData.type)
  },
  onNext:function(event){
    this._updateClassic('next');
  },

  onPrevious:function(event){
    this._updateClassic('previous')
  },

  _updateClassic: function (nextOrPrevious) {
    const index = this.data.classicData.index
    classicModel.getClassic(index,nextOrPrevious,(res) => {
      this._getLikeStatus(res.id,res.type)
      this.setData({
        classicData: res,
        latest: classicModel.isLatest(res.index),
        first: classicModel.isFirst(res.index)
      })
    })
  },
  _getLikeStatus:function(artID,category){
    likeModel.getClassicLikeStatus(artID,category,
    (res)=>{
      this.setData({
        likeCount:res.fav_nums,
        likeStatus:res.like_status
      })
    })
  }

})